<template>
  <div class="roles_container">
    <!-- 自定义面包屑组件 -->
    <my-bread :list="['权限管理', '角色列表']"></my-bread>
    <el-card class="box-card">
      <el-button type="primary" @click="openAddDialog()">添加角色</el-button>
      <el-table :data="rolesList" style="width: 100%" border>
        <el-table-column type="expand">
          <!-- 角色列表（展开内容 权限数据渲染） -->
          <template slot-scope="scope">
            <div v-if="!scope.row.children.length" style="color:#ccc">该角色未分配任何权限</div>
            <div v-if="scope.row.children.length">
              <el-row
                v-for="(item, index) in scope.row.children"
                R
                :key="item.id"
                style="border-bottom:1px solid #eee"
                :style="{
                  'border-top': index === 0 ? '1px solid #eee' : 'none',
                }"
              >
                <el-col :span="4">
                  <el-tag closable @close="delRights(scope.row, item.id)">
                    {{
                    item.authName
                    }}
                  </el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="20">
                  <el-row
                    v-for="(subItem, subIndex) in item.children"
                    :key="subItem.id"
                    :style="{
                      'border-top': subIndex !== 0 ? '1px solid #eee' : 'none',
                    }"
                  >
                    <el-col :span="6">
                      <el-tag
                        type="success"
                        closable
                        @close="delRights(scope.row, subItem.id)"
                      >{{ subItem.authName }}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="18">
                      <el-tag
                        type="warning"
                        closable
                        v-for="lastItem in subItem.children"
                        :key="lastItem.id"
                        @close="delRights(scope.row, lastItem.id)"
                      >{{ lastItem.authName }}</el-tag>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="序号" type="index" width="60"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" prop>
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="openEditDialog(scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="delRole(scope.row.id)">删除</el-button>
            <el-button
              type="warning"
              icon="el-icon-s-tools"
              @click="openRightsDialog(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加角色对话框 -->
    <el-dialog title="添加角色" :visible.sync="addDialogFormVisible">
      <el-form :model="addForm" label-width="80px" autocomplete="off" :rules="rules" ref="addForm">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRole()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑角色对话框 -->
    <el-dialog title="编辑角色" :visible.sync="editDialogFormVisible">
      <el-form
        :model="editForm"
        autocomplete="off"
        label-width="80px"
        :rules="rules"
        ref="editForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editRole()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分配权限对话框 -->
    <el-dialog title="角色分配" :visible.sync="rightsDialogFormVisible">
      <!-- 树状结构数据 -->
      <el-tree
        :data="rightsTreeData"
        show-checkbox
        node-key="id"
        :default-expand-all="true"
        :default-checked-keys="checkedIdArr"
        :props="defaultProps"
        ref="tree"
      ></el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="rightsDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="disRights()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import mixin from "./Roles-Mixin.js";
export default {
  mixins: [mixin]
};
</script>

<style lang="less" scoped>
.el-table .el-button {
  height: 30px;
  font-size: 12px;
  padding-left: 15px;
  padding-right: 15px;
  padding-top: 0px;
  padding-bottom: 0px;
}
.el-row {
  padding: 10px 0px;
  display: flex;
  align-items: center;
}
.el-tag {
  margin-right: 5px;
  margin-bottom: 10px;
}
</style>
